<?php
class Company_model extends CI_Model {

    function __construct()
    {
        parent::__construct();
    }
    
    function get($data)
    {
    	$this->db->select('company.*, company_thumbnail.thumbnail, membership.key, company_size.name AS size,
    					company_category.name AS category, country.name AS country');
    	$this->db->from('company');
    	$this->db->join('membership', 'company.idmember = membership.id');
    	$this->db->join('company_thumbnail', 'company_thumbnail.idmember = membership.id AND company_thumbnail.idnow = \'Y\'', 'left');
    	$this->db->join('company_category', 'company_category.id = company.idcategory', 'left');
    	$this->db->join('company_size', 'company_size.id = company.idsize', 'left');
    	$this->db->join('country', 'country.id = company.idcountry', 'left');
    	
    	if (isset($data['idcompany'])) {
    		$this->db->where('company.id', $data['idcompany']);
    	}
    	
    	return $this->db->get()->row();
    }
    
	function get_profile_data($data)
    {
    	$this->db->select('company.*, company_thumbnail.thumbnail AS thumbnail, company_thumbnail.image AS image, membership.key');
    	$this->db->from('company');
    	$this->db->join('membership', 'company.idmember = membership.id');
    	$this->db->join('company_thumbnail', 'company_thumbnail.idmember = membership.id AND company_thumbnail.idnow = \'Y\'', 'left');
    	
    	if (isset($data['idmember'])) {
    		$this->db->where('company.idmember', $data['idmember']);
    	}
    	
    	return $this->db->get()->row();
    }
    
	function insert($data)
    {
		$data_insert = array(
		   'name' => $data['name'],
		   'idmember' => $data['idmember']
		);
		
		return $this->db->insert('company', $data_insert);
    }
    
	function update($data)
    {
		$data_insert = array(
		   'name' => $data['name'],
		   'description' => $data['description'],
		   'idcard' => $data['idcard']
		);
		
		if (isset($data['birthday']) && is_numeric($data['birthday']))
		{
			$data_insert['birthday'] = $data['birthday'];
		}
		
    	if (isset($data['birthmonth']) && is_numeric($data['birthmonth']))
		{
			$data_insert['birthmonth'] = $data['birthmonth'];
		}
		
   		if (isset($data['birthyear']) && is_numeric($data['birthyear']))
		{
			$data_insert['birthyear'] = $data['birthyear'];
		}
		
    	if (isset($data['idcategory']) && is_numeric($data['idcategory']))
		{
			$data_insert['idcategory'] = $data['idcategory'];
		}
		
   	 	if (isset($data['idsize']) && is_numeric($data['idsize']))
		{
			$data_insert['idsize'] = $data['idsize'];
		}
		
    	if (isset($data['idcountry']) && is_numeric($data['idcountry']))
		{
			$data_insert['idcountry'] = $data['idcountry'];
		}
		
		$this->db->where('id', $data['idcompany']);
		$this->db->where('idmember', $data['idmember']);
		return $this->db->update('company', $data_insert);
    }
    
    function get_complete_profile($data)
    {
    	$this->db->select('company.*, company_thumbnail.image, company_thumbnail.thumbnail, membership.key,
    			country.name as country, company_category.name as category, company_size.name as size');
    	$this->db->join('membership', 'company.idmember = membership.id');
    	$this->db->join('country', 'company.idcountry = country.id', 'left');
    	$this->db->join('company_thumbnail', 'company_thumbnail.idmember = membership.id AND company_thumbnail.idnow = \'Y\'', 'left');
    	$this->db->join('company_category', 'company_category.id = company.idcategory', 'left');
    	$this->db->join('company_size', 'company_size.id = company.idsize', 'left');
    	$this->db->from('company');
    	 
    	if (isset($data['idmember'])) {
    		$this->db->where('company.idmember', $data['idmember']);
    	}
    	 
    	if (isset($data['idcompany'])) {
    		$this->db->where('company.id', $data['idcompany']);
    	}
    	 
    	return $this->db->get()->row();
    }
}
?>